package com.yh.hljapp.fragment;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yh.hljapp.R;
import com.yh.hljapp.base.BaseFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static android.view.View.SCROLLBARS_INSIDE_OVERLAY;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：zhanghang
 * <p>
 * 工程：SC_tribal
 * <p>
 * 文件名：Web_Fragment
 * <p>
 * 创建时间：2017.09.22.16:25
 */

public class Web_Fragment extends BaseFragment {

    @InjectView(R.id.wv_web)
    WebView mWebView;
    @InjectView(R.id.progressBar)
    ProgressBar progressBar;
    private String mFB;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(mActivity, R.layout.webview, null);
        ButterKnife.inject(this, view);
        initView(view);
        initData();
        return view;
    }

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface", "JavascriptInterface"})
    @Override
    public void initView(View view) {
        super.initView(view);
        mFB = getArguments().getString("fB");
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);//支持js的处理
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setUseWideViewPort(true);// 支持双击缩放
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);  //自适应屏幕
//        settings.setLoadWithOverviewMode(true);   //自适应屏幕    bux
//        settings.setSupportZoom(true);//设置是否支持缩放，默认为true。
        settings.setSupportMultipleWindows(true);
        mWebView.setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
//        mWebView.setInitialScale(100);        // 里面的数字代表缩放等级
        mWebView.setHorizontalScrollBarEnabled(false);//水平不显示
        mWebView.setVerticalScrollBarEnabled(false); //垂直不显示

        mWebView.addJavascriptInterface(new Object() {
            //@param message:  html页面传进来的数据
            @JavascriptInterface
            public String getLocationData() {
                return mFB; // 把本地数据弄成json串，传给html
            }

        }, "MyBrowserAPI");//MyBrowserAPI:自定义的js函数名


        //添加客户端支持
        mWebView.setWebViewClient(new WebViewClient() {
            /**
             * 网页开始加载
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }

            /**
             * 网页加载结束
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);
            }

            /**
             * 所有跳转的链接都会在此方法中回调
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl("file:///android_asset/printpage.html");// 把url加载到webView上

                return false;
                // return super.shouldOverrideUrlLoading(view, url);
            }
        });

        mWebView.loadUrl("file:///android_asset/printpage.html");// 把url加载到webView上

//        mWebView.loadUrl("javascript:WarnPrint('" + mFB + "')");//用Android代码调用JavaScript函数
//        mWebView.loadUrl("javascript:Mess('" + 1231 + "')");//用Android代码调用JavaScript函数

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
